#include "DataCenter.h"

DataCenter::DataCenter()
{
    users.push_back(User("zhangsan", "123123", "财务部"));
    users.push_back(User("lucy", "123123", "ICT产品部"));

    rooms.push_back(MeetingRoom(1, 10));
    rooms.push_back(MeetingRoom(2, 20));

    User *user = new User("zhangsan", "123123", "开发部");
    rooms.push_back(MeetingRoom(1, 10, user));

    admins.push_back(Admin("admin", "admin"));
}

DataCenter DataCenter::dataCenter = DataCenter();

DataCenter &DataCenter::getInstance()
{
    return dataCenter;
}

bool DataCenter::addUser(std::string username, std::string password, std::string department)
{
    for (const User &user : users)
    {
        if (user.getUsername() == username)
        {
            return false;
        }
    }
    users.push_back(User(username, password, department));
    return true;
}

bool DataCenter::deleteUser(std::string username)
{
    std::vector<User>::iterator it = users.begin();
    for (; it != users.end(); it++)
    {
        if (it->_username == username)
        {
            break;
        }
    }
    if (it != users.end())
    {
        users.erase(it);
        return true;
    }
    else
    {
        return false;
    }
}

std::vector<User> DataCenter::getUsers()
{
    return std::vector<User>(users);
}

bool DataCenter::addMeetingRoom(int id, int capacity)
{
    std::vector<MeetingRoom>::const_iterator it = std::find_if(rooms.begin(), rooms.end(), [id](const MeetingRoom &room)
                                                               { return id == room.getId(); });
    if (it != rooms.begin())
    {
        rooms.push_back(MeetingRoom(id, capacity));
        return true;
    }
    else
    {
        return false;
    }
}

bool DataCenter::deleteMeetingRoom(int id)
{
    std::vector<MeetingRoom>::const_iterator deletePos = std::find_if(rooms.begin(), rooms.end(), [id](const MeetingRoom &room)
                                                                      { return room.getId() == id; });
    if (deletePos != rooms.end())
    {
        rooms.erase(deletePos);
        return true;
    }
    else
    {
        return false;
    }
}

std::vector<MeetingRoom> DataCenter::getMeetingRooms()
{
    return std::vector<MeetingRoom>(rooms);
}

void DataCenter::clearAllMeetingRoomStatus()
{
    std::for_each(rooms.begin(), rooms.end(), [](MeetingRoom &room)
                  { room.user = nullptr; });
}

int DataCenter::bookMeetingRoom(User &user, int id)
{
    std::vector<MeetingRoom>::iterator it = std::find_if(rooms.begin(), rooms.end(), [id](const MeetingRoom &room)
                                                         { return room.getId() == id; });
    if (it != rooms.end())
    {
        if (it->user != nullptr)
        {
            return Constant::BOOK_FAIL;
        }
        else
        {
            it->user = &user;
            return Constant::BOOK_SUCCESS;
        }
    }
    else
    {
        return Constant::BOOK_NOT_FOUND;
    }
}

int DataCenter::cancelMeetingRoom(const User &user, int id)
{
    std::vector<MeetingRoom>::iterator it = std::find_if(rooms.begin(), rooms.end(), [id](const MeetingRoom &room)
                                                         { return room.getId() == id; });
    if (it != rooms.end())
    {
        if (it->user != &user)
        {
            return Constant::CANCEL_FAIL;
        }
        else
        {
            it->user = nullptr;
            return Constant::CANCEL_SUCCESS;
        }
    }
    else
    {
        return Constant::CANCEL_NOT_FOUND;
    }
}

std::vector<MeetingRoom> DataCenter::bookedMeetingRooms(const User &user)
{
    std::vector<MeetingRoom> bookedVectors{};
    std::copy_if(rooms.begin(), rooms.end(), std::back_inserter(bookedVectors), [&user](MeetingRoom &room)
                 { return room.getUser() == &user; });
    return bookedVectors;
}

User &DataCenter::loginUser(std::string username, std::string password)
{
    for (User &user : users)
    {
        if (user._username == username && user._password == password)
        {
            return user;
        }
    }
    return User::ERROR_USER;
}

Admin &DataCenter::loginAdmin(std::string username, std::string password)

{
    for (Admin &admin : admins)
    {
        if (admin._username == username && admin._password == password)
        {
            return admin;
        }
    }
    return Admin::ERROR_ADMIN;
}
